package haoyou.csnf.manager.module.pm.controller.admin.rulesbasepay;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import haoyou.csnf.manager.framework.common.pojo.PageParam;
import haoyou.csnf.manager.framework.common.pojo.PageResult;
import haoyou.csnf.manager.framework.common.pojo.CommonResult;
import haoyou.csnf.manager.framework.common.util.object.BeanUtils;
import static haoyou.csnf.manager.framework.common.pojo.CommonResult.success;

import haoyou.csnf.manager.framework.excel.core.util.ExcelUtils;

import haoyou.csnf.manager.framework.apilog.core.annotation.ApiAccessLog;
import static haoyou.csnf.manager.framework.apilog.core.enums.OperateTypeEnum.*;

import haoyou.csnf.manager.module.pm.controller.admin.rulesbasepay.vo.*;
import haoyou.csnf.manager.module.pm.dal.dataobject.rulesbasepay.RulesBasepayDO;
import haoyou.csnf.manager.module.pm.service.rulesbasepay.RulesBasepayService;

@Tag(name = "管理后台 - 基本薪资规则")
@RestController
@RequestMapping("/pm/rules-basepay")
@Validated
public class RulesBasepayController {

    @Resource
    private RulesBasepayService rulesBasepayService;

    @PostMapping("/create")
    @Operation(summary = "创建基本薪资规则")
    @PreAuthorize("@ss.hasPermission('pm:rules-basepay:create')")
    public CommonResult<Integer> createRulesBasepay(@Valid @RequestBody RulesBasepaySaveReqVO createReqVO) {
        return success(rulesBasepayService.createRulesBasepay(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新基本薪资规则")
    @PreAuthorize("@ss.hasPermission('pm:rules-basepay:update')")
    public CommonResult<Boolean> updateRulesBasepay(@Valid @RequestBody RulesBasepaySaveReqVO updateReqVO) {
        rulesBasepayService.updateRulesBasepay(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除基本薪资规则")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pm:rules-basepay:delete')")
    public CommonResult<Boolean> deleteRulesBasepay(@RequestParam("id") Integer id) {
        rulesBasepayService.deleteRulesBasepay(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除基本薪资规则")
                @PreAuthorize("@ss.hasPermission('pm:rules-basepay:delete')")
    public CommonResult<Boolean> deleteRulesBasepayList(@RequestParam("ids") List<Integer> ids) {
        rulesBasepayService.deleteRulesBasepayListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得基本薪资规则")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pm:rules-basepay:query')")
    public CommonResult<RulesBasepayRespVO> getRulesBasepay(@RequestParam("id") Integer id) {
        RulesBasepayDO rulesBasepay = rulesBasepayService.getRulesBasepay(id);
        return success(BeanUtils.toBean(rulesBasepay, RulesBasepayRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得基本薪资规则分页")
    @PreAuthorize("@ss.hasPermission('pm:rules-basepay:query')")
    public CommonResult<PageResult<RulesBasepayRespVO>> getRulesBasepayPage(@Valid RulesBasepayPageReqVO pageReqVO) {
        PageResult<RulesBasepayDO> pageResult = rulesBasepayService.getRulesBasepayPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RulesBasepayRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出基本薪资规则 Excel")
    @PreAuthorize("@ss.hasPermission('pm:rules-basepay:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRulesBasepayExcel(@Valid RulesBasepayPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RulesBasepayDO> list = rulesBasepayService.getRulesBasepayPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "基本薪资规则.xls", "数据", RulesBasepayRespVO.class,
                        BeanUtils.toBean(list, RulesBasepayRespVO.class));
    }

}